<?php
	// +----------------------------------------------------------------------
	// | 开发团队信息
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------
	namespace addon\qcloud_sms;

	use AlibabaCloud\Client\AlibabaCloud;
	use AlibabaCloud\Client\Exception\ClientException;
	use AlibabaCloud\Client\Exception\ServerException;
	use Qcloud\Sms\SmsMultiSender;
	use think\Exception;
	use yicmf\addon\controller\Controller;
	use Qcloud\Sms\SmsSingleSender;
	use app\model\ucenter\Sms as SmsModel;
	use think\facade\Log;

	/**
	 * 开发团队信息插件.
	 * @author 微尘 <yicmf@qq.com>
	 */
	class QcloudSms extends Controller
	{

		public function install()
		{
			return true;
		}

		public function uninstall()
		{
			return true;
		}

		public function sendSms($param)
		{
			try {
				if (1) {
					$data['code'] = 0;
					$data['message'] = '验证码' . (isset($param['data']['code']) ? $param['data']['code'] : '');
				} else {
					// 查询接口1数量
					if ('register' == $param['type'] || 'safe_code' == $param['type']  || 'bind' == $param['type'] || 'forget' == $param['type']) {
						$param_sms['code'] = $param['data']['code'];
						$templId = 'SMS_209165006';
					} elseif ('cash' == $param['type']) {
						$params = [
							$param['data']['nickname'],
							$param['data']['cash_sn'],
						];
						$templId = '274800';
					}
//					$sender = new SmsSingleSender('1400164149', '94a88b2a543a679122ae1ddc5ca030dd');
//					$channel = 'qcloud2';
					//            $result['result'] = 0;
					// 注册或者绑定
					// 数组具体的元素个数和模板中变量个数必须一致，例如事例中 templateId:5678对应一个变量，参数数组中元素个数也必须是一个

					AlibabaCloud::accessKeyClient('LTAI4Fyv9qt4gg8Ka3EPF76z', 'UIuMy1Nd6LsKx26PfX0wK4a13aDuno')
						->regionId('cn-hangzhou')
						->asDefaultClient();
						$result = AlibabaCloud::rpc()
							->product('Dysmsapi')
							// ->scheme('https') // https | http
							->version('2017-05-25')
							->action('SendSms')
							->method('POST')
							->host('dysmsapi.aliyuncs.com')
							->options([
								'query' => [
									'RegionId' => "cn-hangzhou",
									'PhoneNumbers' => $param['mobile'],
									'SignName' => '应龙优品',
									'TemplateCode' => $templId,
									'TemplateParam' => json_encode($param_sms),
									'OutId' => $param['id'],
								],
							])
							->request();

//						["RequestId"] => string(36) "8A339DB0-94AC-45F7-894C-79B58ED4ABC1"
//						["Message"] => string(2) "OK"
//						["BizId"] => string(20) "501203510555412674^0"
//						["Code"] => string(2) "OK"
//				dump($result->toArray());
//					$result = $sender->sendWithParam('86', $param['mobile'], $templId, $params, '易之家', '', 'test');
					$result = $result->toArray();
					if ('OK' == $result['Code']) {
						// 设置渠道成功
						$param['channel'] = 'aliyun';
						$param['template'] = $templId;
						// 设置了异步通知之后可以取消此处的记录
						$param['status'] = 1;
						$param['out_id'] = $result['BizId'];
						$param->save();
						$data['code'] = 0;
						$data['message'] = '短信发送成功';
					} else {
						$data['code'] = $result['result'];
						$data['message'] = $result['errmsg'];
					}
				}
			} catch (\Exception $e) {
				// 备用短信渠道
				Log::record($e->getMessage());
				//            $back = new Back();
				//            $result = $back->send($param['mobile'], $param['data']['code']);
				//            if (0 === $result['res_code']) {
				//                $data['code'] = 0;
				//                $data['message'] = '短信发送成功';
				//                $param['channel'] = '189';
				//                // 设置了异步通知之后可以取消此处的记录
				//                $param['status'] = 1;
				//                $param['out_id'] = $result['idertifier'];
				//                $param->save();
				//            } else {
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
				//            }
			}
			return $data;
		}

		/**
		 * 群发短信
		 * @param $param
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 * @datetime: 2020/2/27 15:08
		 */
		public function sendsMultiSms($param)
		{
			try {
				$sender = new SmsMultiSender($this->getConfig('appid'), $this->getConfig('app_key'));
				if ('send' == $param['type']) {
					$templateId = 543539;
				} else {
					$templateId = 543541;
				}
				$result = $sender->sendWithParam('86', $param['mobile'], $templateId, [], '蘑菇算力');
				$result = json_decode($result, true);
				if (0 == $result['result']) {
					// 设置渠道成功
					$data['code'] = 0;
					$data['message'] = '短信发送成功';
				} else {
					$data['code'] = $result['result'];
					$data['message'] = $result['errmsg'];
				}
			} catch (Exception $e) {
				// 备用短信渠道
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}
	}
